package com.fingard.dsp.bank.directbank.cmbc01;

import com.bocom.jump.bp.util.Base64;

import com.fingard.dsp.bank.directbank.bankInterface.IOrderMsg;
import com.fingard.dsp.bank.reqrespobj.ordermsg.ReqOrderMsgQry;
import com.fingard.dsp.bank.reqrespobj.ordermsg.RetAtsOrderMsgDetail;
import com.fingard.dsp.bank.reqrespobj.ordermsg.RetAtsOrderMsgQry;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

@Component("CMBC01OrderMsg")
public class CMBC01OrderMsg extends CMBC01Base implements IOrderMsg{

	@Override
	public RetAtsOrderMsgQry pushOrderMsg(ReqOrderMsgQry p_orderMsg) throws Exception {
		RetAtsOrderMsgQry retAts = new RetAtsOrderMsgQry(p_orderMsg.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
		String tmpRespInfo = "";
		int i = 1;
		try {
			CMBC01Header bankHeader = new CMBC01Header();
			bankHeader.trnCode = "XR205012";
			XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
			//写入通用的头部节点
			writeCommonHeader(xmlWriter, bankHeader);
			// 写入body
			xmlWriter.writeStartElement("xDataBody");
			xmlWriter.writeElementString("dealerCode",p_orderMsg.DealerCode);// 经销商编码
			xmlWriter.writeElementString("orderNum", p_orderMsg.OrderCode);// 订单编号
			xmlWriter.writeElementString("orderTotalValue",p_orderMsg.OrderTotallAmount);// 订单金额合计
			xmlWriter.writeElementString("orderTotalDiscountValue",p_orderMsg.BillMoney);// 订单折后总金额
			xmlWriter.writeElementString("orderCount",p_orderMsg.OrderNum);// 申请总数量
			xmlWriter.writeElementString("orderBillCount",p_orderMsg.OrderNum);//订单融资笔数（即该笔订单对应了几笔融资）
			xmlWriter.writeElementString("orderDate",p_orderMsg.OrderConfirmDate);//订单日期
			while (p_orderMsg.readRecord()) {
			//	xmlWriter.writeStartElement("Detail");
				RetAtsOrderMsgDetail tmpDetail = p_orderMsg.ReqDetail;
				XElement tmpXElement = new XElement("detail", "");
				tmpXElement.addChild(new XElement("rowCount", ""+i++));
				tmpXElement.addChild(new XElement("financeType", getBankConfig().getValueAsString("financeType")));// 融资产品种类（码值：01-流贷；02-法透；03-银票；04-信用证）
				tmpXElement.addChild(new XElement("financeNo", tmpDetail.BillNo));// 票据号
				tmpXElement.addChild(new XElement("billAmount", tmpDetail.BillMoney));// 产品单价
				tmpXElement.addChild(new XElement("billAmountUsed", tmpDetail.BillMoney));// 融资扣款金额
				tmpXElement.addChild(new XElement("remark", tmpDetail.Reserve1));// 备注
				tmpXElement.addChild(new XElement("reserve", tmpDetail.Reserve2));// 保留字段
				xmlWriter.writeXElement(tmpXElement);
			}
			xmlWriter.writeEndElement();
			xmlWriter.writeEndDocument();
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());// 发送银行
			// String[] tmpStrRet = sendToBank(reqXml);
			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String ans_code = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
					String ans_info = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
					if ("0".equals(ans_code)) {
						String rsp_status = tmpBkReader.getFirstText("/CMBC/xDataBody/returnCode");
						String rsp_msg = tmpBkReader.getFirstText("/CMBC/xDataBody/failReason");
						if (rsp_status.equals("01")) {
							tmpRespCode = "0";
							tmpRespInfo = "处理成功";
						} else {
							tmpRespCode = "-1";
							tmpRespInfo = rsp_msg;
						}

					} else {
						tmpRespCode = "-1";
						tmpRespInfo = ans_info;
					}
				} else {
					tmpRespCode = "-1";
					tmpRespInfo = "交易失败";
				}
			} else {
				tmpRespCode = "-1";
				tmpRespInfo = "交易失败";
			}
		} catch (Exception e) {
			tmpRespCode = "-1";
			tmpRespInfo = e.getMessage();
			e.printStackTrace();
		}
		retAts.writeRespCode(tmpRespCode);
		retAts.writeRespInfo(tmpRespInfo);
		retAts.writeEnd();
		return retAts;
	}

}
